package com.zl.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.zl.model.Person;
import com.zl.utils.JsonUtil;

/**
 * User: c3t Date: 13-12-6 Time: 上午8:45
 */
public class PersonJsonServlet extends HttpServlet {

	public  static Map<Integer,List<Person>> ps = new HashMap<>();

	public PersonJsonServlet() {
		List<Person> l = new ArrayList<>();
		l.add(new Person("jack","22","500"));
		l.add(new Person("peter","25","1500"));
		l.add(new Person("jay","27","2500"));
		ps.put(1,l);
		l = new ArrayList<>();
		l.add(new Person("刘德华","52","9500"));
		l.add(new Person("张学友","55","9500"));
		l.add(new Person("黎明","57","9500"));
		ps.put(2,l);
		l = new ArrayList<>();
		l.add(new Person("鸣人","32","500"));
		l.add(new Person("剑刺","25","500"));
		l.add(new Person("123","57","600"));
		ps.put(3,l);
	}

	protected void doRequest(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
   		 resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		int key = Integer.parseInt(req.getParameter("did"));
		List<Person> l = ps.get(key);
		System.out.print(JsonUtil.getInstance().obj2Json(l));
		out.print(JsonUtil.getInstance().obj2Json(l));

  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
      IOException {
    this.doRequest(req, resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
      IOException {
    this.doRequest(req, resp);
  }
}
